package cn.newgrand.netcall.advertisement;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.util.log.Log;

import com.install4j.runtime.installer.helper.Logger;

import cn.newgrand.common.FileUtil;

/**
 * 广告实体类，
 * 
 * 包括:
 * 		广告Id
 * 		广告标题 title 、
 * 		副标题 subtitle 、
 * 		内容content 、
 * 		类型 type,
 * 		
 * 		类型格式定义为 :   广告位置代码_行业代码     
 * 							 
 * 						   广告位置代码  	1-主窗体 广告   2-聊天窗体头部广告   3-聊天窗体底部广告
 * 							行业代码
 * 		广告链接的地址 url
 * 		广告图片 image
 * 
 * 
 * 其中注意的是：image以字符串的格式存储
 * 
 * @author SidneyRen
 * 
 */
public class Advertisement {

	private int adId; 
	private String title;
	private String subtitle;
	private String content;
	private String type;
	private String url;
	private String image;
	private ImageIcon imageIcon;

	public Advertisement(){}
	
	public Advertisement(int adId, String title, String subtitle,
			String content, String type, String url, String imageURL) {
		super();
		this.adId = adId;
		this.title = title;
		this.subtitle = subtitle;
		this.content = content;
		this.type = type;
		this.url = url;
		if(null!=getURL(imageURL))
			this.imageIcon = new ImageIcon(getURL(imageURL));
		else
			this.imageIcon = new ImageIcon();
	}

	public void setAdId(int adId) {
		this.adId = adId;
	}
	
	public int getAdId() {
		return adId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getURL() {
		return url;
	}

	public void setURL(String url) {
		this.url = url;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public String getSubtitle() {
		return subtitle;
	}
	
	public void setImageIcon(ImageIcon imageIcon) {
		this.imageIcon = imageIcon;
	}
	
	public ImageIcon getImageIcon() {
		return imageIcon;
	}
	
	/**
	 * 设置
	 * @param image
	 */
	public void setEnCodeImage(String image) {
		this.image = image;
	}

	/**
	 * 获取广告的图片 二进制格式返回
	 * 
	 * @return 
	 */
	public byte[] getImage() {
		if (null == image) {
			return null;
		}
		return StringUtils.decodeBase64(image);
	}

	/**
	 * 通过URL设置广告图片
	 * 
	 * @param avatarURL
	 *            the url to the image(png,jpeg,gif,bmp)
	 */
	public void setImage(URL imageURL) {
		
		if(null==imageURL) return;
		
		byte[] bytes = new byte[0];
		
		image = (new ImageIcon(imageURL)).toString();
		
		System.out.println(image);
		
//		try {
//			new ImageIcon(imageURL).toString();
//			bytes = FileUtil.getBytes(imageURL);
//			Log.warning("setImage: +" +bytes.length);
////			Log.warning(FileUtil.getBytes("jar:file:/C:/Program%20Files/netcall/lib/spark.jar!/images/close_white.png"));
//			
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		setImage(bytes);
	}

	/**
	 * 通过二进制设置广告图片
	 * 
	 * @param bytes
	 *            the bytes of the avatar.
	 */
	public void setImage(byte[] bytes) {
		if (bytes == null) {
			// Remove avatar (if any) from mappings
			image = " ";
			return ;
		}
		// Otherwise, add to mappings.
		String encodedImage = StringUtils.encodeBase64(bytes);
		image = encodedImage;

	}
	public URL getURL(String imageURL) {
		if(null==getClass().getClassLoader().getResource(imageURL)){
//			Log.error("imageURL is not correct or file is not exist:"+imageURL);
			return null;
		}
//		Log.error("show imageURL :"+getClass().getClassLoader().getResource(imageURL).toString());
        return getClass().getClassLoader().getResource(imageURL);
    }
}
